package com.xieshuai.common.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 字符串工具类
 * @author yxhp
 *
 */
public class StringUtil {
	
	private StringUtil() {};
	/**
	 * 判断传媒的源字符串长度是否大于0
	 * @param src
	 * @return
	 */
	public static boolean hasLength(String src) {
		return src != null && src.length()>0;
	}
	/**
	 * 判断传入的源字符串是否为非空
	 * @param src
	 * @return
	 */
	public static boolean isNotEmpty(String src) {
		boolean result = false;
		
		//判断src是都为null,或者是否为空,如果是返回false;
		
		//判断haslength方法是否为空,不为空返回true
		result = StringUtil.hasLength(src);
		//!true
		if(!result) {
			//返回false
			return result;
		}else {
			//判断src中所有字符是否全部为空格,如果是则返回false;
			//将字符串转换为字符数组
			//遍历这个字符数组,如果发现一个非空字符,则将result设置为true,然后退出循环
			
			//重新初始化
			result = false;
			
			char[] temp = src.toCharArray();
			
			for(int i=0;i<temp.length;i++) {
				if(temp[i] != ' ') {
					result = true;
					break;
				}
			}
		}
		return result;
	}
	/**
	 * 判断传入的是否为手机号
	 * @param mobile
	 * @return
	 */
	public static boolean isMobileNumber(String mobile) {
		return mobile.matches("^1[3|4|5|7|8][0-9]\\d{4,8}$");
	}
	
	/**
	 * <--**判断是否为全字母**-->
	 * @param src
	 * @return
	 */
	public static boolean isAllAlphbet(String src) {
		boolean result = false;
		
		//编译不通过返回false,通过就继续执行
		if(!StringUtil.isNotEmpty(src)) {
			return result;
		}
		
		char[] temp = src.toCharArray();
		
		for(char c : temp) {
			//如果不是字母就返回false
			if(!Character.isAlphabetic(c)) {
				result = false;
				break;
			}
			result = true;
		}
		
		return result;
	}
	
	/**
	 * <--**循环生成随即字母**-->
	 * @param n
	 * @return
	 */
	public static String genRandomString(int n) {
		StringBuffer buffer = new StringBuffer();
		
		for(int i = 0 ;i<n;i++) {
			//random生成0-1(不包含一的随机数)
			char randomChar = (char) ((Math.random()*58)+65);
			randomChar = randomChar>90 && randomChar <97 ? (char)(randomChar+8):randomChar;
			buffer.append(randomChar);
		}
		return buffer.toString();
	}
	
	
	/**
	 * 生成随机的汉字
	 * @param n
	 * @return
	 */
	public static String genChineseString(int n) {
		StringBuffer buffer = new StringBuffer();
		
		int start = Integer.valueOf("4e00",16);
		int end = Integer.valueOf("9fa5",16);
		
		for(int i =0 ; i<n;i++) {
			char randomChar = (char)((Math.random()*(end-start+1))+start);
			buffer.append(randomChar);
		}
		return buffer.toString();
	}
	
	
	/**
	 * 计算年龄
	 * @param birthday
	 * @return
	 */
	public static int calcAge(Calendar birthday) {
		if(birthday == null) {
			return 0;
		}
		int age = 0;
		
		//获取日历中的年月日信息
		int year = birthday.get(Calendar.YEAR);
		int month = birthday.get(Calendar.MONTH);
		int date = birthday.get(Calendar.DATE);
		
		Calendar toDay = new GregorianCalendar();
		
		age = toDay.get(Calendar.YEAR) - year;
		
		if(month > toDay.get(Calendar.MONTH)) {
			age--;
		}else if(month == toDay.get(Calendar.MONTH)) {
			if(date > toDay.get(Calendar.DATE)) {
				age--;
			}
		}
		return age;
	}
	
	/**
	 * 计算未来时间到今天时间的隔天数
	 */
	public static int calcDaysFromFuture(Date future) {
		int result = 0;
		long futrueTime = future.getTime();
		long today = new Date().getTime();
		
		result = (int)((futrueTime - today)/1000/60/60/24);
		return result+1;
	}
	
}
